package ink.mint;


import ink.mint.activation.ActivationType;
import ink.mint.convolution.*;
import ink.mint.dataset.MnistRead;
import ink.mint.loss.LossType;
import ink.mint.matrix.MatrixExtensions;
import ink.mint.matrix.Matrixable;
import ink.mint.modes.MultilayeredPerceptronNeuralNetworks;
import ink.mint.neural.Layer;
import ink.mint.dataset.ResourceLoader;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Objects;

/**
 * DeepLearning for Java by inkdyes
 * 个人网站: https://morn.ink
 */
public class App {


    /**
     * 判断软件是否打包安装运行
     */
    static boolean isNoPackageOrPackage = false;
    /**
     * 安装包用户路径
     */
    static String UserDirPath = "";
    /**
     * 墨染的主方法
     *
     * @param args 参数
     */
    public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {

        MultilayeredPerceptronNeuralNetworks multilayeredPerceptronNeuralNetworks = new MultilayeredPerceptronNeuralNetworks();
        multilayeredPerceptronNeuralNetworks.run();

        String TRAIN_IMAGES_FILE = isNoPackageOrPackage ? "src/main/resources/DataSet/train-images/": String.valueOf(ResourceLoader.getResourcePath("/app/DataSet/train-images/"));
        String TRAIN_LABELS_FILE = isNoPackageOrPackage ? "src/main/resources/DataSet/train-labels/" :  String.valueOf(ResourceLoader.getResourcePath("/app/DataSet/train-labels/"));
        String TEST_IMAGES_FILE = isNoPackageOrPackage ? "src/main/resources/DataSet/test-images/" :  String.valueOf(ResourceLoader.getResourcePath("/app/DataSet/test-images/"));
        String TEST_LABELS_FILE = isNoPackageOrPackage ? "src/main/resources/DataSet/test-labels/" :  String.valueOf(ResourceLoader.getResourcePath("/app/DataSet/test-labels/"));

        double[][][] X = MnistRead.getImagesMatrix(readFullFilePath(TRAIN_IMAGES_FILE));
        double[][][] XT = MnistRead.getImagesMatrix(readFullFilePath(TEST_IMAGES_FILE));

        double[] Y = MnistRead.getLabels(readFullFilePath(TRAIN_LABELS_FILE));
        double[] YT = MnistRead.getLabels(readFullFilePath(TEST_LABELS_FILE));



        ConvolutionLayer cl1 = new ConvolutionLayer();
        cl1.setConvolutionNeuralNumber(6);
        cl1.setConvolutionLayerParameter(5, 1, 4, ActivationType.RELU);
        cl1.setPoolingLayerParameter(2, 2, 0, PoolingType.MaxPooling);
        ConvolutionLayer cl2 = new ConvolutionLayer();
        cl2.setConvolutionNeuralNumber(16);
        cl2.setConvolutionLayerParameter(5, 1, 0, ActivationType.RELU);
        cl2.setPoolingLayerParameter(2, 2, 0, PoolingType.MaxPooling);


        Layer layer1 = new Layer(120, ActivationType.Sigmoid);
        Layer layer2 = new Layer(84, ActivationType.Sigmoid);
        Layer layer3 = new Layer(10, ActivationType.Sigmoid);

//        ConvolutionLayerConfiguration clc = new ConvolutionLayerConfiguration(X, Y);
//        clc.convolutionLayerConfiguration(cl1);
//        clc.convolutionLayerConfiguration(cl2);

//        clc.fullyConnectedLayerConfiguration(layer1);
//        clc.fullyConnectedLayerConfiguration(layer2);
////        clc.fullyConnectedLayerConfiguration(layer3);
//        clc.learningRateConfiguration(0.05);
//        clc.lossConfiguration(LossType.SoftmaxLossFunction);
//        clc.saveParameter("src/main/resources/DataSet/CNN.mode");
//        clc.train();

//        ConvolutionLayerConfiguration clc1 = new ConvolutionLayerConfiguration(XT, YT);
//        clc1.readParameter("src/main/resources/DataSet/CNN.mode");
//        clc1.run();
    }


    public static String readFullFilePath(String filepath) throws FileNotFoundException, IOException {
        File file = null;
        File readfile = null;
        try {
            file = new File(filepath);
            if (!file.isDirectory()) {
                file.getPath();
                file.getAbsolutePath();
                file.getName();
            } else if (file.isDirectory()) {
                String[] filelist = file.list();
                for (int i = 0; i < filelist.length; i++) {
                    readfile = new File(filepath + "/" + filelist[i]);
                    if (!readfile.isDirectory()) {
                        readfile.getPath();
                        readfile.getAbsolutePath();
                        readfile.getName();
                    } else if (readfile.isDirectory()) {
                        readFullFilePath(filepath + "/" + filelist[i]);
                    }
                }
            }
        } catch (FileNotFoundException e) {
            System.out.println("readfile()   Exception:" + e.getMessage());
        }
        return readfile.getPath();
    }
}